<?php


require('smarty/libs/Smarty.class.php');
define('SITE_PATH', getcwd());

class CommonController extends Smarty
{
    var $db;
    function __construct()
    {
        parent::__construct();
        $this->smarty_config();
        $this->db_config();
        $this->sideBar();
    }
    function smarty_config(){
        $this->setTemplateDir(SITE_PATH . '/templates/');
        $this->setCompileDir(SITE_PATH . '/templates/templates_c/');
        $this->setConfigDir(SITE_PATH . '/configs/');
        $this->setCacheDir(SITE_PATH . '/cache');
        $this->left_delimiter = "{{";
        $this->right_delimiter = "}}";
    }
    function db_config(){
        $this->db = new mysqli("localhost", "root", "", "blog");
        if (mysqli_connect_errno()) {
            exit("连接失败: %s<br>". mysqli_connect_error());
        }
        $this->db->query("set names utf8");


    }
    function sideBar(){
        $tags = $this->getAll("select * from tag");

        $order =$this->getAll("select * from article order by time desc limit 3");
        $this->assign("tags",$tags);
        $this->assign("order",$order);
    }


    function getAll($sql){

        $arr = [];
        // global $mysqli;
        $result =  $this-> query($sql);
        while($row =$result->fetch_assoc()){

            $arr[] = $row;
        }
        return $arr;
    }

    function getOne($sql){
        //  global $mysqli;
        $result = $this->query($sql);
        return $result->fetch_assoc();
    }

    function redirect($url='',$info=''){


        echo "<script>alert('".$info."');location.href='$url'</script>";

    }


    function query($sql){

        $result = $this->db->query($sql);
        return $result;
    }



}


function dump($arr){
     echo "<pre>";
     print_r($arr);
     echo "</pre>";
}

?>